package com.hxx.day8;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;

public class TestDemo1 {
    /**
     * 测试TreeSet的排序
     */
    @Test
    public void test1(){
        TreeSet<Book> books = new TreeSet<>((x,y)->y.getId()-x.getId());
        books.add(new Book(1,"book1",100,"author1"));
        books.add(new Book(2,"book2",200,"author2"));
        books.add(new Book(3,"book3",300,"author3"));
        System.out.println(books);
    }
    /**
     * 过滤
     */
     @Test
    public void test2(){
         List<Book> list = List.of(new Book(1, "book1", 100, "author1"),
                 new Book(2, "book2", 200, "author2"),
                 new Book(3, "book3", 300, "author3"));
         //转换
         list.stream()
                 .filter(x->x.getPrice()>100)
                 .forEach(System.out::println);
     }

    /**
     * 按照价格排序，如果价格相同则按照图书名称排序
     */
     @Test
    public void test3(){
        List<Book> list = new ArrayList<>();
        list.add(new Book(1, "java", 100, "author1"));
        list.add(new Book(2, "mysql", 200, "author2"));
        list.add(new Book(3, "spring", 300, "author3"));
        list.add(new Book(4, "html", 200, "author4"));

        //使用集合的帮助类
         Collections.sort(list,(x,y)->{
             int i = Double.compare(y.getPrice(), x.getPrice());
             if(i!=0){
                 return i;
             }
             return y.getName().compareTo(x.getName());
         });
        System.out.println(list);

    }
}


























